package com.kun.commonutil.exceptionhandler;


import com.kun.commonutil.exception.HospitalException;
import com.kun.commonutil.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

/**
 * @Author zhoukun
 * @Date 2021/3/22 12:39
 * 全局异常处理类
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 统一异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result<String> GlobalError(Exception e)
    {
        log.error("全局处理异常{}"+ Arrays.toString(e.getStackTrace()));
        e.printStackTrace();
        return   Result.fail("全局异常错误信息："+e.getMessage());
    }

    /**
     * 自定义异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(HospitalException.class)
    public Result<String> GlobalError(HospitalException e)
    {
        log.error("特殊异常错误信息{}"+ Arrays.toString(e.getStackTrace()));
        e.printStackTrace();
        return   Result.fail("特殊异常错误信息:"+ e.getMessage());
    }
}
